package com.example.administrator.myapplication.activity.albumActivity.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.myapplication.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.albumActivity.bean.programEntity.ProgramEntity;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.FragmentProgramBinding;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProgramFragment extends Fragment implements Callback<ProgramEntity> {
     private  FragmentProgramBinding binding;
    private CommentRecyclerAdapter<Object> adapter;
    private XmlyService service;

    public ProgramFragment() {
        // Required empty public constructor
    }

    public static ProgramFragment newInstance(int  albumId) {
        Bundle args = new Bundle();
        args.putInt("albumId",albumId);
        ProgramFragment fragment = new ProgramFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return DataBindingUtil.inflate(inflater,R.layout.fragment_program, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = DataBindingUtil.getBinding(view);
        aboutRecycler();
    }

    private void aboutRecycler() {
        int albumId = getArguments().getInt("albumId");
        HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap<>();
        map.put(null,Pair.create(R.layout.album_program_item1, BR.trackData1));
        adapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(),map);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getContext().getApplicationContext()).getService();
        service.getProgramEntity(albumId,20).enqueue(this);
    }

    @Override
    public void onResponse(Call<ProgramEntity> call, Response<ProgramEntity> response) {
            adapter.addAll(response.body().getData().getTracks().getList());
    }

    @Override
    public void onFailure(Call<ProgramEntity> call, Throwable t) {
           t.getMessage();
       // Toast.makeText(getContext(),t.getMessage(),Toast.LENGTH_LONG).show();
    }
}
